package com.ruoyi.system.flowable.service.impl;

import com.ruoyi.system.domain.FlowableActRuTask;
import com.ruoyi.system.flowable.service.IFlowableService;
import com.ruoyi.system.mapper.FlowableRuMapper;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 参数配置 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class FlowableServiceImpl implements IFlowableService
{
    @Autowired
    private FlowableRuMapper flowableRuMapper;

    @Autowired
    ProcessEngine processEngine;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    IdentityService identityService;

    @Override
    public int delete_deploy(String flow_def_id) {
        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("num", 5);
        vars.put("assign1", "user1");
        repositoryService.deleteDeployment(flow_def_id);
        return 1;
    }

    @Override
    public List<Map> selectFlowableActRuTaskList(FlowableActRuTask ruTask)
    {
        return flowableRuMapper.selectFlowableActRuTaskList();
    }

    @Override
    public List<Map> selectFlowableActReProcDefList(){
        return flowableRuMapper.selectFlowableActReProcDefList();
    }

    @Override
    public List<Map> select_flowable_act_re_deployment_pagedata(){
        return flowableRuMapper.select_flowable_act_re_deployment_pagedata();
    }

    @Override
    public int proc_inst_task_complete(String proc_inst_id) {
        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("num", 5);
        vars.put("assign1", "user1");
        taskService.complete(proc_inst_id, vars);
        return 1;
    }

    @Override
    public int proc_inst_task_start(String flow_def_id)
    {
        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("num",5);
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(flow_def_id);
        return 1;
    }

    @Override
    public String deploy_flow(String flow_def_key, String flow_def_name,String flow_text){
        Deployment deploy = repositoryService.createDeployment()
                .addString(flow_def_name, flow_text).name(flow_def_name).deploy();
//        Deployment deploy = repositoryService.createDeployment()
//                .addClasspathResource("static/flowXml/new0825.bpmn20.xml").deploy();
        return deploy.getId();
    }


    @Override
    public String get_deploy_flow_by_id(String flow_def_id){
//            // 使用历史流程实例查询流程定义
//            ProcessDefinition processDefinition = processEngine.getRepositoryService()
//                    .createProcessDefinitionQuery()
//                    .processDefinitionId(flow_def_id)
//                    .singleResult();

            // 获取流程定义的XML
            String processDefinitionXml = new BufferedReader( new InputStreamReader(
                    processEngine.getRepositoryService().getProcessModel(flow_def_id))).lines().collect(Collectors.joining("\n"));
        return processDefinitionXml;
    }
}
